package com.itheima.controller ;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class checkItemController {
    @Autowired
  private   CheckItemService checkItemService;

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result FindAll(){
        List<CheckItem> list = checkItemService.findAll();
        log.info("检查项为:{}",list);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    /**
     * 通过id查询检查项
     * @param id
     * @return
     */

    @GetMapping("/findById")
    public Result FindById(Integer id){
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);

    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param checkgroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
            List<Integer> list = checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        log.info("参数：{}",id);
         Boolean temp =    checkItemService.delete(id);
        return new Result(temp,temp?MessageConstant.DELETE_CHECKGROUP_SUCCESS:MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    /**
     * 新增
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        boolean temp = checkItemService.add(checkItem);
        return new Result(temp,temp?MessageConstant.ADD_CHECKITEM_SUCCESS:MessageConstant.ADD_CHECKITEM_FAIL);
    }

    /**
     * 编辑
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑信息:{}",checkItem);
        boolean temp =checkItemService.edit(checkItem);
        return new Result(temp,temp?MessageConstant.EDIT_CHECKITEM_SUCCESS:MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkItemService.findPage(queryPageBean);
    }

}
